// export const getWeekDate = () => {
//   const date = new Date()
//   const year = date.getFullYear()
//   const month = date.getMonth() + 1
//   const day = date.getDate()
//   const hour = date.getHours()
//   const minute = date.getMinutes()
//   const second = date.getSeconds()

//   return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
// }
export function getWeekDate(i = 0) {
  const weeks = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  const cur = new Date()
  const tar = new Date(cur.getTime() + 24 * 60 * 60 * 1000 * i)
  const date = tar.getDate()
  const mth = (tar.getMonth() + 1)
  const hour = tar.getHours()
  const minute = tar.getMinutes()
  const second = tar.getSeconds()
  const obj = {
    day: date == 1 ? ((tar.getMonth() + 1) + '.' + date) : (date > 9 ? date : '0' + date),
    week: weeks[tar.getDay()],
    cur: tar.getFullYear() + '-' + (mth > 9 ? mth : ('0' + mth)) + '-' + (date > 9 ? date : '0' + date)
      + ' ' + (hour > 9 ? hour : ('0' + hour)) + ':' + (minute > 9 ? minute : ('0' + minute)) + ':' + (second > 9 ? second : ('0' + second)),
    cur1: tar.getFullYear() + '-' + (mth > 9 ? mth : ('0' + mth)) + '-' + date
  }
  return obj
}
export function setCache(key, value) {
  try {
    wx.setStorageSync(key, value)
  } catch (e) {}
}
export function getCache(key) {
  try {
    var value = wx.getStorageSync(key)
    if (value) {
      return value;
    }
  } catch (e) {}
}

export function removeCache(e, t) {
  wx.removeStorage({
    key: e,
    success: t
  })
}

export function previewImage(url, urls) {
  if (url.indexOf("?") != -1) {
    var end = url.indexOf("?");
    url = url.substr(0, end);
  }
  for (var i in urls) {
    if (urls[i].indexOf("?") != -1) {
      var end = urls[i].indexOf("?");
      urls[i] = urls[i].substr(0, end)
    }
  }
  wx.previewImage({
    current: url, // 当前显示图片的http链接
    urls: urls, // 需要预览的图片http链接列表
  })
}

export function getSceneParams(scene) {
  let params = getParas(scene)
  let page = getPage(params.f)
  let pagePras = getPageParas(params)

  let sceneParams = {
    uid: params.s || 0,
    params: params,
    share_page: '/pages/' + page + (params.p ? pagePras : '') + '&puid=' + (params.s || 0) + '&hide=1'
  }
  return sceneParams
}

function strToObj(str,seperator1,seperator2) {
  // 拆分键值对数组
  let arr = str.split(seperator1);
  // 对象
  var obj = {};
  for (var j = 0; j < arr.length; j++) {
    // 其它参其中一对
    var str3s = arr[j];
    // 拆分键值数组
    var para = str3s.split(seperator2);
    // 对象赋值
    obj[para[0]] = para[1];
  }
  return obj;
}
function getParas(scene) {
  // scene=f_g-s_274-p_g.2528*p.128
  var obj = strToObj(scene,'-','_');
  if (obj.p){
    obj.p = strToObj(obj.p,'*','.');
  }
  return obj;
}
function getPage(name) {
  var obj = {
    'p': 'pos/posInfo/index',
    't': 'pos/ticketInfo/index',
    'i':'index/index',
    'gd':'shop/gdinfo/index',
  };
  return obj[name];
}
function getPageParas(obj) {
  // gooddetail
  //p : { g: "2528", p: "128" }
  var paras = obj.p;
  var dic = {
    // 钓场详情
    'p': {
      'g': 'id'
    },
    't': {
      'g': 'id'
    }
  }
  // 拿到参数对照表
  var mirror = dic[obj.f];
  // 参数字符串
  var paraStr = '?';

  // 拼参
  for (var k in paras) {
    paraStr = paraStr + mirror[k] + '=' + paras[k] + '&';
  }

  return paraStr;
}
